/**
 * 
 */
package com.mago.tis.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mago.tis.bean.TradersBean;
import com.mago.tis.service.ICompanyService;
import com.mago.tis.serviceImpl.CompanyService;
import com.mago.tis.pojo.Tiscit;
import com.mago.tis.utility.ApplicationConstant;
import com.mago.tis.utility.ApplicationException;
import com.mago.tis.utility.ExceptionMessage;
import com.mago.tis.utility.StringUtil;

/**
 * @author Yogesh
 * 
 * It is used to Company Information on Challan third.
 * 
 */
public class CompanyController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(CompanyController.class);
	
	private ICompanyService companyService=null;
	private String message = "";
	public CompanyController(){
		companyService=new CompanyService();
	}
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.info("CompanyController controller called");
	
	
		String action=request.getParameter(ApplicationConstant.ACTION);
		String companyId=request.getParameter(ApplicationConstant.COMP_ID);
		
		String name=request.getParameter(ApplicationConstant.COMPANY_NAME);
		String address=request.getParameter(ApplicationConstant.COMPANY_ADDRESS);
		
		String about=request.getParameter(ApplicationConstant.ABOUT);
		String resultingPage="";
		if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.UPDATE_COMPANY)) {
			boolean isUpdateCompany=false;
			Tiscit tiscit=new Tiscit();
			tiscit.setCompanyId(Integer.parseInt(companyId));
			tiscit.setCompanyName(name);
			tiscit.setAddress(address);
			try {
				isUpdateCompany=companyService.updateCompany(tiscit);
				if(isUpdateCompany){
					message=ApplicationConstant.UPDATE_COMPANY_SUCCESSFULL;
					logger.info("CompanyController UpdateCompany()--->"+message);
					response.sendRedirect(request.getContextPath()+"/adminEntry?ACTION=CINFO");
					
				}
				
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController UpdateCompany()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController UpdateCompany()--->"+e);
				e.printStackTrace();
			}		

		}else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.COMPANY_INFO)) {
			
			List<Tiscit> companyInfo = new ArrayList<Tiscit>();
			try {
				companyInfo = companyService.getCompanyInfoById("1");
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController Get_Company_Info()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController Get_Company_Info()--->"+e);
				e.printStackTrace();
			}
			request.setAttribute("companyInfo", companyInfo);
			resultingPage = "pages/jsp/adminThirdChallanEntry.jsp";
			rd = request.getRequestDispatcher(resultingPage);
			rd.forward(request, response);
				
		}
		else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.UPDATE_ABOUT)) {
			boolean isUpdateAbout=false;
			Tiscit tiscit=new Tiscit();
			tiscit.setCompanyId(Integer.parseInt(companyId));
			tiscit.setAbout(about);
			try {
				isUpdateAbout=companyService.updateAbout(tiscit);
				if(isUpdateAbout){
					message=ApplicationConstant.UPDATE_ABOUT_SUCCESSFULL;
					logger.info("CompanyController updateAbout()--->"+message);
					response.sendRedirect(request.getContextPath()+"/adminAboutEntry?ACTION=ABINFO&message="+message);
				}
				
			} catch (ApplicationException e) {
				message=ExceptionMessage.UPDATE_ABOUT_UNSUCCESSFULL;
				logger.error("Exception in CompanyController updateAbout()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UPDATE_ABOUT_UNSUCCESSFULL;
				logger.error("Exception in CompanyController updateAbout()--->"+e);
				e.printStackTrace();
			}		

		}
		else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.ABOUT_INFO)) {
			
			List<Tiscit> companyInfo = new ArrayList<Tiscit>();
			try {
				companyInfo = companyService.getCompanyInfoById("1");
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController Get_Company_Info()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController Get_Company_Info()--->"+e);
				e.printStackTrace();
			}
			request.setAttribute("companyInfo", companyInfo);
			resultingPage = "pages/jsp/adminAbout.jsp";
			rd = request.getRequestDispatcher(resultingPage);
			rd.forward(request, response);
				
		}
		else if (StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.COMPANY_ABOUT)) {
			
			List<Tiscit> companyInfo = new ArrayList<Tiscit>();
			try {
				companyInfo = companyService.getCompanyInfoById("1");
			} catch (ApplicationException e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController Get_Company_Info()--->"+e);
				e.printStackTrace();
			}catch (Exception e) {
				message=ExceptionMessage.UNSUCCESSFULL_COMPANY_TRADER;
				logger.error("Exception in CompanyController Get_Company_Info()--->"+e);
				e.printStackTrace();
			}
			request.setAttribute("companyInfo", companyInfo);
			resultingPage = "pages/jsp/about.jsp";
			rd = request.getRequestDispatcher(resultingPage);
			rd.forward(request, response);		
		}
		
	}

}
